package Controllers

//  redis
import (
	"context"
	"fmt"
	"math/rand"
	"strings"
	"time"
	"yz-go/global"

	"github.com/gin-gonic/gin"
	"github.com/go-redis/redis/v8"
)

func GenerateSmSCode(width int) string {
	numeric := [10]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
	r := len(numeric)
	rand.Seed(time.Now().UnixNano())

	var sb strings.Builder
	for i := 0; i < width; i++ {
		fmt.Fprintf(&sb, "%d", numeric[rand.Intn(r)])
	}
	return sb.String()
}

func RedisTest(c *gin.Context) {

	rdb := redis.NewClient(&redis.Options{
		Addr: fmt.Sprintf("%s:%d", global.Config.RedisInfo.Host, global.Config.RedisInfo.Prot),
		DB:   0,
	},
	)
	mobile := "13666666666"
	smsCode := GenerateSmSCode(6)
	r := rdb.Set(context.Background(), mobile, smsCode, 120*time.Second) // 保存120秒
	fmt.Println(r)
}
